package com.gqr.threadcoreknowledge.sixstates;

/**
 * Copyright (C), 2020-2024,gqr
 * FileName:BlockedWaitingTimedWaiting
 * author:guqingrong
 * Date:2024-04-20 17:34
 * Description:展示Blocked,Waiting,TimedWating
 * History:
 * <author>    <time>    <version>    <desc>
 * 作者姓名     修改时间     版本号        描述
 */
public class BlockedWaitingTimedWaiting implements Runnable {

    public static void main(String[] args) throws InterruptedException {
        BlockedWaitingTimedWaiting runnable = new BlockedWaitingTimedWaiting();

        Thread thread1 = new Thread(runnable);
        thread1.start();
//        Thread.sleep(5);
        Thread thread2 = new Thread(runnable);
        thread2.start();
        // 打印出TIMED_WAITING 因为正在执行Thread.sleep(2000)
        System.out.println(thread1.getState());
        // 打印出BLOCKED 因为thread2想拿syn的锁却拿不到
        System.out.println(thread2.getState());

        Thread.sleep(1300);
        // 打印出WAITING状态
        System.out.println(thread1.getState());
    }

    @Override
    public void run() {
        syn();
    }

    private synchronized void syn() {
        try {
            Thread.sleep(1000);
            wait();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
